package com.cxyzy.demo

import android.content.Context
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.View
import android.widget.ScrollView

class TestScroll(context: Context, attrs: AttributeSet) : ScrollView(context, attrs) {

    private val mGestureDetector: GestureDetector

    init {
        mGestureDetector = GestureDetector(YScrollDetecotr())
        setFadingEdgeLength(0)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev)
    }

    internal inner class YScrollDetecotr : SimpleOnGestureListener() {
        override fun onScroll(e1: MotionEvent, e2: MotionEvent,
                              distanceX: Float, distanceY: Float): Boolean {
            // if(distanceY != 0 && distanceX != 0){
            //
            // }
            // if(Math.abs(distanceY) >= Math.abs(distanceX)){
            // System.out.println("distanceX = " + distanceX + " , distanceY = "
            // + distanceY);
            // return true;
            // }
            // return false;

            val angle = Math.atan2(Math.abs(distanceY).toDouble(), Math.abs(distanceX).toDouble())
            //			System.out.println("angle-->" + (180 * angle) / Math.PI);
            return if (180 * angle / Math.PI < 180) {
                false
            } else false

        }
    }

    companion object {
        private val SCROLL_ANGLE = 90.0
    }
}